﻿-- =============================================
-- Script Template
-- =============================================
USE [DB_Ex]
GO

/****** Object:  StoredProcedure [dbo].[sp_Avaya_Calls_LastMonth]    Script Date: 12/05/2011 10:41:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_Avaya_Calls_LastMonth]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Declare @dt as datetime;
SELECT @dt = DATEADD(month, -3, SYSDATETIME());
Declare @dtFrom as date;
Declare @dtFrom2 as datetime;
SELECT @dt = DATEADD(day, (DAY(@dt)*-1+1), @dt);
select @dtFrom = CAST(@dt as date);
select @dtFrom2 = CAST(@dtFrom as datetime);

SELECT
	  ID
	, SDATE as Дата
	, SDURA as Всего_секунд
	, ( (SDURA/60.0) ) as Время_мин
	, SCALLER as Исходящий_N
	, STNUMBER
	, SDIALER as Входящий_N
	, SCODEUSE
	, SODCODE
	, SINCRT
	, SOUTCRT
	, SNUM
	, SLOGIN as Линия
	, SOU as Отдел
FROM DB01.avaya.dbo.row AS calls
WHERE (SODCODE <> 4) AND (SDATE >= @dtFrom2)
-- ORDER BY SDATE Desc

END

GO


